<template>
    <div class="sl-top">
        <div>
            <el-button-group size="small">
                <el-button :icon="SetUp"></el-button>
                <!--<el-button :icon="ArrowLeft"></el-button>-->
                <!--<el-button :disabled="true" :icon="ArrowRight"></el-button>-->
            </el-button-group>
        </div>
        <div>
            <el-button-group size="small">
                <template v-for="item in platformData">
                    <el-button
                        :icon="item.icon"
                        :type="item.active === true ? 'primary' : ''"
                        @click="platform(item.type)"
                    ></el-button>
                </template>
            </el-button-group>
        </div>
        <div>
            <el-button type="primary" :icon="View" size="small" @click="preview">预览</el-button>
            <el-popconfirm @confirm="delAll" title="请确认是否清理舞台">
                <template #reference>
                    <el-button type="danger" :icon="DeleteFilled" size="small">清除</el-button>
                </template>
            </el-popconfirm>
        </div>
    </div>
    <div class="content">
        <el-form v-bind="formPropsBing">
            <div :class="['form', platformClass]" id="form">
                <template v-if="RootNode.length === 0">
                    <el-empty description="请拖拽左侧组件到舞台中"></el-empty>
                </template>
                <template v-else>
                    <template v-for="item in RootNode" :key="item">
                        <FormItemView :id="item" />
                    </template>
                </template>
            </div>
        </el-form>
    </div>
</template>

<script lang="ts" setup>
import { DeleteFilled, View, ArrowLeft, ArrowRight, SetUp } from '@element-plus/icons-vue'
import { computed, onMounted, reactive, ref, watch } from 'vue'
import { RootNode, delAll } from '../lib/form'
import { initTargetDrag } from '../lib/drag'
import { mainFormData } from '../lib'

import FormItemView from './form_item.vue'

const platformClass = ref('')
const platformData = reactive([
    { icon: 'monitor', type: 'pc', active: true, class: '' },
    { icon: 'cellphone', type: 'pad', active: false, class: 'pad-layout' },
    { icon: 'iphone', type: 'phone', active: false, class: 'phone-layout' },
])
const formPropsBing = reactive<{ [keys: string]: any }>({
    labelSuffix: '：',
})

const platform = (type: string = '') => {
    for (const key in platformData) {
        platformData[key].active = false
        if (platformData[key].type === type) {
            platformData[key].active = true
            platformClass.value = platformData[key].class
        }
    }
}

const parserProps = () => {
    const formPropKeyMap = ['size', 'labelPosition', 'labelWidth']
    for (const key in mainFormData) {
        if (formPropKeyMap.includes(key)) {
            formPropsBing[key] = mainFormData[key]
        }
    }
}

watch(
    () => mainFormData,
    () => {
        parserProps()
    },
    { deep: true },
)

const preview = () => {}

onMounted(() => {
    initTargetDrag()
})
</script>

<style scoped lang="scss">
.sl-top {
    height: 39px;
    background-color: #ffffff;
    display: flex;
    justify-content: space-between;
    border-bottom: 1px solid #ccc;
    align-items: center;
    padding: 0 20px;
}
.content {
    height: 100%;
    padding: 10px;
    :deep(.el-form) {
        height: calc(100% - 40px - 20px - 20px);
    }
    .form {
        height: 100%;
        max-height: calc(1000px - 80px);
        background-color: #fff;
        margin: 0 auto;
        overflow-y: auto;
        padding: 10px;
        :deep(.el-form-item--default) {
            padding-bottom: 18px;
            margin-bottom: 0;
        }
    }
    .pad-layout {
        max-width: 960px;
        border-radius: 10px;
        box-shadow: 0 0 1px 10px #495060;
    }
    .phone-layout {
        width: 420px;
        border-radius: 10px;
        box-shadow: 0 0 1px 10px #495060;
    }
}
</style>

<style lang="scss">
// 拖拽开始的样式
.sl-item div.sortable-ghost {
    // border: 1px dashed #000000;
    text-indent: 3px;
    box-sizing: border-box;
    content: '';
    overflow: hidden;
    padding: 0;
}

// 拖拽到目标后的样式信息 后续处理
#form > .sortable-chosen {
    border: 1px dashed #009688;
    background-color: #e9f4fd;
    &.item {
        display: flex;
        align-items: center;
        .icon > span {
            font-size: 20px;
        }
        .title {
            font-size: 14px;
            margin-left: 10px;
        }
    }
}
</style>
